<template>
	<view>
		<page-meta>
			<navigation-bar/>
		</page-meta>

    <u-toast ref="uToast" />
    <view class="img-a">
      <!-- <view class="t-b">
        您好，
        <br />
        欢迎使用 开源字节
      </view> -->
    </view>
    <view class="login-view">
      <view class="t-login">
        <form class="cl">
          <view class="t-a">
            <text class="txt">手机号</text>
            <input type="number" name="phone" placeholder="请输入您的手机号" maxlength="11" v-model="username" />
          </view>
          <view class="t-a">
            <text class="txt">密码</text>
            <input type="password" name="code" maxlength="18" placeholder="请输入您的密码" v-model="password" />
          </view>
          <button @click="login()">登 录</button>
          <!-- <view class="reg" @tap="reg()">短信登录</view> -->
          <!-- <view class="reg" @click="reg()">注 册</view> -->
        </form>
      </view>
    </view>
    <!-- #ifdef MP-WEIXIN -->
    <view>
      <button open-type="getPhoneNumber" @getphonenumber="weChatLogin" class="clearBtn">
        <view class="loginType">

            微信登录测试

        </view>
      </button>
<!--       <view class="hint">-->
<!--        登录代表同意-->
<!--        <text class="link">开源字节用户协议、隐私政策，</text>-->
<!--        并授权使用您的账号信息（如昵称、头像、收获地址）以便您统一管理-->
<!--      </view>-->
    </view>
    <!-- #endif -->
	</view>
</template>

<script>
import {fun1} from "../components/tools/pageData.js";
import obj from "../components/tools/pageData.js";
export default {
		data() {
			return {
				hello:'hello',
        username: '',
        password: '',
			}
		},
    mounted: function(){
      this.hello = obj.name
    },
		methods: {
      weChatLogin(e){
        let code = e.detail.code
        console.log(code)

        uni.showModal({    //提示框用法
          title: '提示',
          content: code,
          cancelText: '取消',
          confirmText: '确认',
        })


      },
      wChatLogin(e) {
        let code = e.detail.code
        console.log(e)
        if (code) {
          const url = '/tianHe-home/v3/api-docs/user/loginOrRegister'
          this.$u.post(url, {
            code: code
          }).then(data => {
            console.log(data)
            this.$u.vuex('vuex_token', data.data.token);
            this.$u.vuex('vuex_user', data.data.loginInfo);
            //判断是否是注册
            const isRegister = data.data.isRegister
            //如果是第一次登录 弹出提示
            if(isRegister){
              uni.showModal({
                title: '提示',
                content: '初始密码为123456,请尽快修改!',
                cancelText: '取消',
                confirmText: '确认',
                success: res => {
                  return uni.reLaunch({
                    url: '/pages/index/index'
                  })
                }
              })
            }else{
              uni.reLaunch({
                url: '/pages/index/index'
              })
            }
          })
        } else {
          this.$mytip.toast("登录失败")
        }
      },
      login() {
        if (!this.$u.test.mobile(this.username)) {
          return this.$refs.uToast.show({
            title: '手机号不正确',
            type: 'warning',
          })
        }
        if (!this.password) {
          return this.$refs.uToast.show({
            title: '密码不能为空',
            type: 'warning',
          })
        }
        // 登录json参数，不同于表单参数
        let url = "/tianHe-home/v3/api-docs/user/login";
        this.$u.post(url, {
          username: this.username,
          password: this.password,
          loginType: 1,
          loginSource: ''
        }).then(data => {
          // 登录成功初始化token与用户信息
          this.$u.vuex('vuex_token', data.data.token);
          this.$u.vuex('vuex_user', data.data.loginInfo);
          console.log(data.data.loginInfo)
          // this.$u.vuex('vuex_codeAtWill', data.codeAtWill);
          console.log('登录信息')
          console.log(data)
          uni.reLaunch({
            url: '/pages/index/index'
          })
        });
      },
      reg() {
        this.$u.route({
          url: 'pages/login/register'
        })
      }
		}
	}
</script>

<style>

page {
  background-color: #ffffff;
}
.txt {
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
}

.img-a {
  width: 100%;
  height: 450rpx;
  background-image: url(https://zhoukaiwen.com/img/loginImg/head.png);
  background-size: 100%;
}

.reg {
  font-size: 28rpx;
  color: #fff;
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 50rpx;
  font-weight: bold;
  background: #f5f6fa;
  color: #000000;
  text-align: center;
  margin-top: 30rpx;
}

.login-view {
  width: 100%;
  position: relative;
  margin-top: -120rpx;
  background-color: #ffffff;
  border-radius: 8% 8% 0% 0;
}

.t-login {
  width: 600rpx;
  margin: 0 auto;
  font-size: 28rpx;
  padding-top: 80rpx;
}

.t-login button {
  font-size: 28rpx;
  background: #2796f2;
  color: #fff;
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 50rpx;
  font-weight: bold;
}

.t-login input {
  height: 90rpx;
  line-height: 90rpx;
  margin-bottom: 50rpx;
  border-bottom: 1px solid #e9e9e9;
  font-size: 28rpx;
}

.t-login .t-a {
  position: relative;
}

.t-b {
  text-align: left;
  font-size: 42rpx;
  color: #ffffff;
  padding: 130rpx 0 0 70rpx;
  font-weight: bold;
  line-height: 70rpx;
}

.t-login .t-c {
  position: absolute;
  right: 22rpx;
  top: 22rpx;
  background: #5677fc;
  color: #fff;
  font-size: 24rpx;
  border-radius: 50rpx;
  height: 50rpx;
  line-height: 50rpx;
  padding: 0 25rpx;
}

.t-login .t-d {
  text-align: center;
  color: #999;
  margin: 80rpx 0;
}

.t-login .t-e {
  text-align: center;
  width: 250rpx;
  margin: 80rpx auto 0;
}

.t-login .t-g {
  float: left;
  width: 50%;
}

.t-login .t-e image {
  width: 50rpx;
  height: 50rpx;
}

.t-login .t-f {
  text-align: center;
  margin: 150rpx 0 0 0;
  color: #666;
}

.t-login .t-f text {
  margin-left: 20rpx;
  color: #aaaaaa;
  font-size: 27rpx;
}

.t-login .uni-input-placeholder {
  color: #aeaeae;
}

.cl {
  zoom: 1;
}

.cl:after {
  clear: both;
  display: block;
  visibility: hidden;
  height: 0;
  content: '\20';
}

.clearBtn {
  margin: 20px;
  padding: 0;
  line-height: 1;
  background-color: #FFFFFF;
}

.clearBtn::after {
  position: unset !important;
  border: unset;
}
</style>
